package com.hongyuan.coach.base;

import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import androidx.databinding.BindingAdapter;
import com.bumptech.glide.Glide;
import com.hongyuan.coach.R;
import com.hongyuan.coach.data.md5.BaseUtil;
import com.hongyuan.coach.myview.CircularStatisticsView2;
import com.hongyuan.coach.myview.VersionChangeView;
import com.hongyuan.coach.myview.risenumber_textview.RiseNumberTextView;
import com.hongyuan.coach.ui.main.beans.CheckVersionBeans;

import java.io.File;

public class MyBindingAdapter {

    @BindingAdapter(value = {"imageUrl", "defaultImg"}, requireAll = false)
    public static void bindImageUrl(ImageView view, Object imageUrl, Drawable defaultImg){
        if(BaseUtil.isValue(imageUrl)){
            Glide.with(view.getContext()).load(imageUrl).into(view);
        }else{
            if(defaultImg != null){
                Glide.with(view.getContext()).load(defaultImg).into(view);
            }else{
                Glide.with(view.getContext()).load(R.mipmap.fang_default).into(view);
            }
        }
    }

    @BindingAdapter("imageFile")
    public static void bindImageUrl(ImageView view, File imageFile){
        if(!BaseUtil.isValue(imageFile))return;

        Glide.with(view.getContext()).load(imageFile).into(view);
    }


    @BindingAdapter("intImg")
    public static void bindIntImg(ImageView img,int imgId){
        if(imgId != 0){
            img.setImageResource(imgId);
        }else{
            img.setImageResource(R.color.theme_FFFFFF);
        }

    }


    @BindingAdapter("setIntText")
    public static void bindPraiseImg(TextView textView, int nums){
        textView.setText(String.valueOf(nums));
    }

    @BindingAdapter("setAppChangeDate")
    public static void bindUpdateAppData(VersionChangeView view, CheckVersionBeans.InfoBean infoBean){
        if(infoBean != null && infoBean.getIs_new() == 1){
            view.startChange(infoBean);
        }else{
            return;
        }
    }

    @BindingAdapter("setIntBg")
    public static void bindViwBg(View view, int bgId){
        view.setBackgroundResource(bgId);
    }

    @BindingAdapter("setMyTextColor")
    public static void bindTextColorBg(TextView view, int bgId){
        view.setTextColor(view.getResources().getColor(bgId));
    }

    @BindingAdapter("setSexImg")
    public static void bindSexImg(ImageView img,int sex){
        if(sex == 1){
            img.setImageResource(R.mipmap.person_boby_mark_img);
        }else{
            img.setImageResource(R.mipmap.person_girl_mark_img);
        }
    }

    @BindingAdapter("setRNTextNum")
    public static void bindRiseNumTextView(RiseNumberTextView view, String num){
        if(Float.parseFloat(num) <= 0){
            return;
        }
        view.withNumber(Float.parseFloat(num)).start();
    }

    @BindingAdapter(value = {"setCSPNum1", "setCSPNum2"}, requireAll = false)
    public static void bindCSPercentage(CircularStatisticsView2 view, String num1,String num2){
        view.setPercentage(Float.parseFloat(num1),Float.parseFloat(num2));
    }

    @BindingAdapter("setRadioGroupCheck")
    public static void bindRadioGroupCheck(RadioGroup view, int checkId){
        if(checkId == -1){
            view.clearCheck();
        }else{
            view.check(checkId);
        }
    }
}
